package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.QCformEntity;

import java.util.ArrayList;
import java.util.List;

public class QCformSpcLineItemAdapter extends BaseQuickAdapter<QCformEntity.QCFORMINFO.SPCLINESINFO.QCFORMSPCLINE, BaseViewHolder> {

    public QCformSpcLineItemAdapter(@Nullable ArrayList<QCformEntity.QCFORMINFO.SPCLINESINFO.QCFORMSPCLINE> data) {
        super(R.layout.item_spc_line, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, QCformEntity.QCFORMINFO.SPCLINESINFO.QCFORMSPCLINE item) {
        helper.setText(R.id.tvSeqNo,item.getSEQNO());
        helper.setText(R.id.tvDcName,item.getDCNAME());
        helper.setText(R.id.etDcData,item.getDCDATA());

        EditText etDcData = helper.getView(R.id.etDcData);
        setEditListener(etDcData,item);
    }

    private void setEditListener(EditText editText, QCformEntity.QCFORMINFO.SPCLINESINFO.QCFORMSPCLINE item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    item.setDCDATA(s.toString());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
